package 商城进货交易记录程序设计;

import java.io.File;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

public class SuffixNameSearch implements INoperation{
    @Override
    public void workFIle() {
        //后缀名检索
        System.out.println("请输入要检索的目录");
        Scanner sc = new Scanner(System.in);
        String directory = sc.nextLine();
        System.out.println("请输入要检索的后缀名");
        String extensions = sc.nextLine();

        File file = new File(directory);
        if(!file.exists() || !file.isDirectory())
        {
            System.out.println("目录不存在");
            return;
        }
        System.out.println("检索结果为：");
        SearchbyExtension(file, extensions);
    }

    public void SearchbyExtension(File fileDirectory, String extensions)
    {
        String[] extensionArray = extensions.split(",");
        //重写拆分名称方法
        File[] files = fileDirectory.listFiles((dir, name) ->{
            String fileExtension = name.substring(name.lastIndexOf(".") + 1);
            return Arrays.asList(extensionArray).contains(fileExtension);
        });
        if(files != null)
        {
            for(File file : files)
            {
                System.out.println(file.getAbsolutePath());
            }
        }
    }
}
